/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.util.logsystem;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;


/**
 * <p><code>DiskFile</code> is an implementation of <code>OutputDevice</code> to output the log data to the disk
 * file with a given name. default log file name is: ChronicleLog.txt</p>
 *  <p>Instance of <code>ThisClass</code> is immutable and thread safe</p>
 *
 * @author Sowrov
 * @version %I% %G%
 */
public class DiskFile implements OutputDevice {
    private BufferedWriter output;
    private String filePathName;
    private boolean freshFile;

    /**
     * Default constructor. Construct a DiskFile Object with file name = "ChronicleLog.txt";
     */
    public DiskFile() {
        this.freshFile = true;
        this.filePathName = "ChronicleLog.txt"; //default value
        this.output = null;
    } // end DiskFile()

    /**
     * Construct a DiskFile object with a given file name
     * @param fileName
     */
    public DiskFile(final String fileName) {
        this.freshFile = true;
        this.filePathName = fileName;
        this.output = null;
    } // end DiskFile()

    public boolean isFreshDevice() {
        return this.freshFile;
    } // end isFreshDevice()

    public void close() {
        try {
            this.output.close();
        } // end try
        catch (final IOException ex) {
        } // end catch
    } // end close()

    public void flush() {
        try {
            this.output.flush();
        } // end try
        catch (final IOException ex) {
        } // end catch
    } // end flush()

    public void init() {
        try {
            this.output = new BufferedWriter(new FileWriter(this.filePathName));
            this.freshFile = true;
        } // end try
        catch (final IOException ex) {
            System.out.println("Couldn't init file");
        } // end catch
    } // end init()

    public synchronized void write(final String str) {
        this.freshFile = false;

        if (this.output != null) {
            try {
                this.output.write(str);
                this.flush();
            } // end try
            catch (final IOException ex) {
                System.out.println("Write error");
            } // end catch
        } // end if
    } // end write()
} // end DiskFile
